<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;

class BaseController extends Controller
{
    protected $model;
    protected $error;
    protected $assign;

    // 初始化
    public function initialize()
    {
        $myUser = $this->myUser();
        $action = $this->optLogic()->getData('action', 'index', 'trim');

        /*header("Location: /organ/home/index");
        die;*/

        /*if(empty($myUser) && (!in_array($action, ['login', 'on-login']))){
            // 如果没有登录
            header("Location: /home/login");
            header("Location: /organ/home/index");
            die;
        }*/

        parent::initialize();
    }

    // 获取用户以及权限
    protected function myUser()
    {
        $option = $this->optLogic();
        $info   = $option->session();
        if($info === false){
            //$option->throwIf(empty($user), $option->getError(), $option->getCode());
            return $info;
        }
        if(empty($info)){
            return $info;
        }
        // 组合
        $info['uid']  = $info['id'];
        $this->myUid  = $info['id'];
        $this->myUser = $info;
        $option->assign('myUser', $info);

        return $info;
    }

    /*// 是否手机端访问
    public function isMobile()
    {
        if(isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")){
            return true;
        } elseif(isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")){
            return true;
        } elseif(isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])){
            return true;
        } elseif(isset($_SERVER['HTTP_USER_AGENT']) && preg_match(
                '/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i',
                $_SERVER['HTTP_USER_AGENT']
            )){
            return true;
        }

        return false;
    }

    // 返回数据
    protected function assign($name, $value = '')
    {
        if(is_array($name)){
            $this->assign = array_merge($this->assign, $name);
        } else{
            $this->assign[$name] = $value;
        }

        return $this;
    }

    protected function getAssign($name, $value = '')
    {
        return isset($this->assign[$name]) ? $this->assign[$name] : $value;
    }

    // 加载模板输出
    protected function dispaly($template, $vars = [])
    {
        $vars = empty($vars) ? $this->assign : array_merge($this->assign, $vars);
        $vars = empty($vars) ? [] : $vars;
        if($this->isMobile()){
            $template = 'wap/'.$template;
        } else{
            $template = 'pc/'.$template;
        }

        return view($template, $vars);
    }*/
}


